<template>
  <view class="container">
    <view class="content">
      <!-- 用户信息卡片 -->
      <view class="user-card">
        <view class="user-info">
          <image 
            class="avatar" 
            :src="userInfo.avatarUrl || defaultAvatar"
            @error="handleAvatarError"
          ></image>
          <view class="user-detail">
            <text class="nickname">{{ userInfo.nickname || '未登录' }}</text>
            <text class="user-desc" v-if="isLoggedIn">已完成{{ userInfo.completedSurveys || 0 }}份问卷</text>
            <button class="login-btn" v-if="!isLoggedIn" @click="login">点击登录</button>
          </view>
        </view>
      </view>

      <!-- 功能菜单 -->
      <view class="menu-list">
        <view class="menu-group">
          <view class="menu-item" @click="navigateTo('/pages/my/history')">
            <text class="menu-icon" style="color: #2979FF;">📅</text>
            <text class="menu-text">答卷历史</text>
            <text class="arrow-icon">→</text>
          </view>
          <view class="menu-item" @click="navigateTo('/pages/my/analysis')">
            <text class="menu-icon" style="color: #FF9500;">📊</text>
            <text class="menu-text">睡眠分析</text>
            <text class="arrow-icon">→</text>
          </view>
          <view class="menu-item" @click="navigateTo('/pages/my/favorite')">
            <text class="menu-icon" style="color: #FF5A5F;">⭐</text>
            <text class="menu-text">我的收藏</text>
            <text class="arrow-icon">→</text>
          </view>
        </view>
        
        <view class="menu-group">
          <view class="menu-item" @click="navigateTo('/pages/my/notification')">
            <text class="menu-icon" style="color: #07C160;">🔔</text>
            <text class="menu-text">消息通知</text>
            <text class="arrow-icon">→</text>
          </view>
          <view class="menu-item" @click="navigateTo('/pages/my/settings')">
            <text class="menu-icon" style="color: #666;">⚙️</text>
            <text class="menu-text">设置</text>
            <text class="arrow-icon">→</text>
          </view>
          <view class="menu-item" @click="navigateTo('/pages/my/about')">
            <text class="menu-icon" style="color: #2979FF;">ℹ️</text>
            <text class="menu-text">关于我们</text>
            <text class="arrow-icon">→</text>
          </view>
        </view>
      </view>

      <!-- 版本信息 -->
      <view class="app-info">
        <text>睡眠健康小助手 v1.0.0</text>
      </view>
    </view>

    <!-- 底部导航栏 -->
    <view class="tab-bar">
      <view class="tab-item" @click="handleTabClick('home')">
        <text class="tab-icon">🏠</text>
        <text class="tab-text">首页</text>
      </view>
      <view class="tab-item" @click="handleTabClick('survey')">
        <text class="tab-icon">📋</text>
        <text class="tab-text">问卷</text>
      </view>
      <view class="tab-item" @click="handleTabClick('record')">
        <text class="tab-icon">✅</text>
        <text class="tab-text">记录</text>
      </view>
      <view class="tab-item" @click="handleTabClick('knowledge')">
        <text class="tab-icon">📚</text>
        <text class="tab-text">知识</text>
      </view>
      <view class="tab-item active" @click="handleTabClick('my')">
        <text class="tab-icon" style="color: #2979ff;">👤</text>
        <text class="tab-text" style="color: #2979ff;">我的</text>
      </view>
    </view>
  </view>
</template>

<script>
import { ref, computed, onMounted } from 'vue';

export default {
  setup() {
    const userInfo = ref({
      nickname: '张三',
      avatarUrl: '',
      completedSurveys: 5
    });

    const defaultAvatar = '/image/article/default-avatar.png';
    const isLoggedIn = computed(() => !!userInfo.value.nickname);

    // 处理头像加载错误
    const handleAvatarError = () => {
      userInfo.value.avatarUrl = defaultAvatar;
    };

    // 登录
    const login = () => {
      uni.navigateTo({
        url: '/pages/login/index'
      });
    };

    // 页面跳转
    const navigateTo = (url) => {
      uni.navigateTo({
        url: url
      });
    };

    // 处理标签点击
    const handleTabClick = (tab) => {
      switch (tab) {
        case 'home':
          uni.navigateTo({ url: '/pages/index/index' });
          break;
        case 'survey':
          uni.navigateTo({ url: '/pages/survey/index' });
          break;
        case 'record':
          uni.navigateTo({ url: '/pages/record/index' });
          break;
        case 'knowledge':
          uni.navigateTo({ url: '/pages/knowledge/index' });
          break;
        case 'my':
          // 当前已在我的页面，不需要跳转
          break;
      }
    };



    // 获取用户信息
    const getUserInfo = async () => {
      // 实际开发中应该从本地存储或接口获取用户信息
      const token = uni.getStorageSync('token');
      if (!token) {
        userInfo.value = {
          nickname: '',
          avatarUrl: '',
          completedSurveys: 0
        };
      }
    };

    onMounted(() => {
      getUserInfo();
    });

        return {
      userInfo,
      defaultAvatar,
      isLoggedIn,
      handleAvatarError,
      login,
      navigateTo,
      handleTabClick
    };
  }
};
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #f5f5f5;
}

.content {
  flex: 1;
  padding: 20rpx;
  padding-bottom: 120rpx;
}

.user-card {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 40rpx 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 20rpx rgba(0, 0, 0, 0.08);
}

.user-info {
  display: flex;
  align-items: center;
}

.avatar {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  margin-right: 30rpx;
  background-color: #f0f0f0;
}

.user-detail {
  flex: 1;
}

.nickname {
  font-size: 34rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 12rpx;
  display: block;
}

.user-desc {
  font-size: 26rpx;
  color: #999;
}

.login-btn {
  margin-top: 16rpx;
  background-color: #2979ff;
  color: #fff;
  font-size: 28rpx;
  padding: 8rpx 20rpx;
  border-radius: 100rpx;
  line-height: 1.5;
  display: inline-block;
}

.menu-list {
  margin-bottom: 30rpx;
}

.menu-group {
  background-color: #fff;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 20rpx rgba(0, 0, 0, 0.08);
  overflow: hidden;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1px solid #f5f5f5;
}

.menu-item:last-child {
  border-bottom: none;
}

.menu-text {
  flex: 1;
  font-size: 30rpx;
  color: #333;
  margin-left: 20rpx;
}

.menu-icon {
  font-size: 22rpx;
  width: 44rpx;
  text-align: center;
}

.arrow-icon {
  font-size: 16rpx;
  color: #999;
}

.app-info {
  text-align: center;
  padding: 40rpx 0;
  color: #999;
  font-size: 24rpx;
}

.tab-bar {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 100rpx;
  background-color: #fff;
  border-top: 1px solid #eee;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}

.tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10rpx 0;
}

.tab-icon {
  font-size: 32rpx;
  color: #666;
  margin-bottom: 8rpx;
  line-height: 1;
}

.tab-text {
  font-size: 20rpx;
  color: #666;
  line-height: 1;
}

.tab-item.active .tab-text {
  color: #2979ff;
}


</style> 